package com.example.graduatetest.model;

import android.util.Log;

import com.example.graduatetest.bean.Student;
import com.example.graduatetest.bean.Teacher;
import com.example.graduatetest.callback.LoginCallBack;
import com.example.graduatetest.callback.StringCallBack;
import com.example.graduatetest.callback.StudentCallBack;
import com.example.graduatetest.callback.TeacherCallBack;
import com.example.graduatetest.utils.Constant;
import com.google.gson.Gson;

import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class StudentModel {
    private static String www = Constant.www;
    private static String changePasswordUrl = "/student/changePassword";
    private static String selectOneStudentUrl = "/student/selectOneStudent";


    public static void selectOneStudent(final String student_id,final StudentCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                    RequestBody body = new FormBody.Builder()
                            .add("student_id", student_id)
                            .build();
                    Log.e("haha", student_id);

                    Request request = new Request.Builder()
                            .url(www+selectOneStudentUrl)
                            .post(body)
                            .build();//创建一个Request对象

                    Response response = client.newCall(request).execute();//发送请求获取返回数据
                    String responseData = response.body().string();//处理返回的数据
                    Log.e("haha", responseData);
                    parseJSONStudent(responseData, callBack);
                }catch (Exception e){
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }
    //修改密码 网络请求
    public static void changePassword(final String number, final String password,final StringCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                    RequestBody body = new FormBody.Builder()
                            .add("number", number)
                            .add("password", password)
                            .build();
                    Log.e("haha", password);

                    Request request = new Request.Builder()
                            .url(www+changePasswordUrl)
                            .post(body)
                            .build();//创建一个Request对象

                    Response response = client.newCall(request).execute();//发送请求获取返回数据
                    String responseData = response.body().string();//处理返回的数据
                    Log.e("haha", responseData);
                    callBack.onSuccess(responseData);

                }catch (Exception e){
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();

    }

    //解析Student json对象
    private static void parseJSONStudent(String jsonData, StudentCallBack callBack){
        try{
            JSONObject jsonObject = new JSONObject(jsonData);
            Gson gson = new Gson();//实例化gson
            //解析Classes对象
            Log.e("haha", jsonObject.toString());
            Student student = gson.fromJson(jsonObject.toString(), Student.class);
            callBack.onSuccess(student);
        }catch (Exception e){
            callBack.onFailed();
            e.printStackTrace();
        }
    }
}
